package com.jspxcms.core.web.fore;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.fasterxml.jackson.databind.util.JSONWrappedObject;
import com.jspxcms.common.util.JsonResponse;
import com.jspxcms.common.util.Random;
import com.jspxcms.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by yuanyue on 14-8-23.
 */
@Controller
public class MobileController {


    @RequestMapping("/mobile/send.jspx")
    public void sendMsg(String mobile, HttpServletResponse response, HttpServletRequest request) {
        int status = userService.userExist(null, mobile, null).getKey();
        if (userService.userExist(null, mobile, null).getKey() < 0) {
            JsonResponse.jsonOutput(response, status, "该手机已注册");
            return;
        }
        String mobile_key = Random.getRandom(6);
        request.getSession().setAttribute("mobile_key", mobile_key);
        String result = "success";
        try {
            result = send(mobile_key, mobile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JsonResponse.jsonOutput(response, 1, result);
    }


    private static final String addr = "http://api.sms.cn/mt/";

    private static final String userId = "21045";

    /*
     * 如uid是：test，登录密码是：123123
       pwd=md5(123123test),即
       pwd=b9887c5ebb23ebb294acab183ecf0769
       线生成地址：http://www.sms.cn/password
     */
    private static final String pwd = "c5a66472eaf2dfb8ba68c7aa77e71c4a";

    private static final String encode = "utf8";

    public static String send(String code, String mobile) throws Exception {

        String msgContent = "珍医网为您提供最新资讯、病例讨论、会议视频直播、论文写作指导、文献检索等功能。欢迎您的加入，验证码为:" + code;
        //组建请求
        String straddr = addr +
                "?uid=" + userId +
                "&pwd=" + pwd +
                "&mobile=" + mobile +
                "&encode=" + encode +
                "&content=" + msgContent;

        StringBuffer sb = new StringBuffer(straddr);
        System.out.println("URL:" + sb);

        //发送请求
        URL url = new URL(sb.toString());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                url.openStream(), "gbk"));

        //返回结果
        String inputline = in.readLine();
        return inputline;
    }

    public static String sendPasswordCode(String code, String mobile) throws Exception {

        String msgContent = "珍医网：您的找回密码的验证码为" + code;
        //组建请求
        String straddr = addr +
                "?uid=" + userId +
                "&pwd=" + pwd +
                "&mobile=" + mobile +
                "&encode=" + encode +
                "&content=" + msgContent;

        StringBuffer sb = new StringBuffer(straddr);
        System.out.println("URL:" + sb);

        //发送请求
        URL url = new URL(sb.toString());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                url.openStream(), "gbk"));

        //返回结果
        String inputline = in.readLine();
        return inputline;
    }

    @Autowired
    UserService userService;

}
